﻿using GN.Pay.TradeChannel.UnionPay.Response;
using GN.Pay.Utils.Net.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace GN.Pay.TradeChannel.UnionPay.Request
{
    [Serializable]
    class QueryRequest : APayRequest
    {
        private ITradeChannel channel;
        private UnionPayConfigure config;

        public QueryRequest(ITradeChannel channel,
             UnionPayConfigure config,
             string orderNo)
        {
            this.channel = channel;
            this.config = config;
            orderId = orderNo;
            Init();
        }

        /// <summary>
        /// 商户订单号
        /// </summary>
        public string orderId { get; set; }

        /// <summary>
        /// 订单发送时间
        /// </summary>
        public string txnTime { get; set; }

        public void Init()
        {
            this.merId = config.ConfigureInfo.AccountId;
            this.version = "5.0.0";
            this.encoding = "UTF-8";
            this.signMethod = "01";
            this.txnType = "00";
            this.txnSubType = "00";
            this.bizType = "000000";
            this.accessType = "0";
            this.orderId = orderId;
            this.txnTime = DateTime.Now.ToString("yyyyMMddHHmmss");
        }

        /// <summary>
        /// 生成
        /// </summary>
        /// <returns></returns>
        public QueryResponse Build()
        {
            this.txnTime = DateTime.Now.ToString("yyyyMMddHHmmss");
            var param = this.BuildSignDictionary(channel, config, "signature");
            param = param.Clone(null, v =>
            {
                return HttpUtility.UrlEncode(v, config.Encoding);
            });
            string responseString = HttpUtils.PostString("https://gateway.95516.com/gateway/api/backTransReq.do", param, config.Encoding);
            return new QueryResponse(responseString, channel, config);
        }
    }
}
